<template>
    <el-card>
        <el-table :data="tableData" :header-cell-style="{ 'text-align': 'center', 'font-size': '14px' }"
            :cell-style="{ 'text-align': 'center' }" style="width: 100%;height: 71vh;">
            <el-table-column fixed prop="did" label="药品编号" />
            <el-table-column prop="dname" label="药品名称" />
            <el-table-column prop="dbrand" label="药品品牌" />
            <el-table-column prop="stock" label="库存量" />
            <el-table-column prop="addin" label="申请数量" show-overflow-tooltip />
            <el-table-column prop="applytime" label="申请日期" show-overflow-tooltip width="230" />
            <el-table-column prop="addstaff" label="申请人" show-overflow-tooltip />
            <el-table-column prop="applyaddress" label="申请地点" show-overflow-tooltip />
            <el-table-column prop="status" label="状态">
                <template #default="props">
                    <el-tag :type="props.row.status === 1 ? 'primary' : 'danger'">
                        {{ props.row.status == 1 ? "待审核" : "已拒绝" }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template #default="props">
                    <el-button link type="primary" size="small" @click="handlePassed(props.row.did)">通过
                    </el-button>
                    <el-button link type="primary" size="small" @click="handleRefuse(props.row.did)">拒绝
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
</template>
<script>
import { applyList, applyEdit, applyRefused } from "@/api/comm";
import { FindInfo } from '@/api/login.js'
import { ElMessage, ElMessageBox } from "element-plus";
export default {
    data() {
        return {
            registerMsg: {},
            dialogDrugsApplyVisible: false,
            tableData: []
        }
    },
    created() {
        this.queryApplyList();
    },
    methods: {
        queryApplyList() {
            applyList().then((res) => {
                this.tableData = res.data.data
            })
        },
        handlePassed(did) {
            for (let index = 0; index < this.tableData.length; index++) {
                if (this.tableData[index].did === did) {
                    this.registerMsg = JSON.parse(JSON.stringify(this.tableData[index]))
                    break
                }
            }
            this.username = localStorage.getItem("username");
            FindInfo(this.username).then(res => {
                this.address = res.data.data.address
                this.registerMsg.dfrom = this.address;
            })
            this.registerMsg.did = did;
            applyEdit(this.registerMsg).then((res) => {
                this.$message({
                    type: "success",
                    message: "审核通过!"
                })
                this.queryApplyList();
            })

        },
        handleRefuse(did) {
            ElMessageBox.prompt("请输入拒绝理由", "Tip", {
                confirmButtonText: "拒绝",
                cancelButtonText: "关闭",
            })
                .then(({ value }) => {
                    ElMessage({
                        type: "success",
                        message: `已拒绝`,
                    });
                    this.registerMsg.msg = value;
                    this.registerMsg.did = did;
                    this.registerMsg.status = 2;
                    return applyRefused(this.registerMsg);
                })
                .then(() => {
                    this.queryApplyList();
                })
                .catch(() => {
                    ElMessage({
                        type: "info",
                        message: "取消成功",
                    });
                });
        }
    }
}
</script>
<style lang="scss" scoped>
.blue {
    color: blue;
}

.red {
    color: red;
}

::v-deep .registerMsg .el-form-item__label {
    width: 105px;
}

.registerMsg .el-input {
    width: 300px;
}
</style>